#include "../../global/QmlNames.h"
#include "../../util/PhotoUtil.h"
#include "../../persist/DbManager.h"
#include "../RosterModel.h"
#include "ContactModel.h"

#include <QSqlQuery>

X_BEGIN_NAMESPACE

///////////////////////////////////////////////////////////
// ContactModel，只存储好友的名片信息，不存储非好友的名片信息
///////////////////////////////////////////////////////////
ContactModel::ContactModel()
    : QObject(NULL)
    , d(new ContactModelPrivate)
{
}

ContactModel::ContactModel(const QString &bareJid)
    : QObject(NULL)
    , d(new ContactModelPrivate)
{
    d->m_bareJid = bareJid;
}

ContactModel::ContactModel(const ContactModel &src)
    : QObject(NULL)
    , d(src.d)
{
    if(this == &src) return;

    d->m_availableStatus = src.availableStatus();
    d->m_bareJid = src.bareJid();
}

ContactModel &ContactModel::operator =(const ContactModel &rhs)
{
    if(this != &rhs) d = rhs.d;

    return *this;
}

void ContactModel::saveVCard()
{

}

/**
 * @brief 从QXmppVCardIq获取数据并更新联系人数据。
 *
 * @param iq
 */
void ContactModel::updateFromXmppIq(const QXmppVCardIq &vcardIQ)
{
    d->m_nickName = vcardIQ.nickName();
    d->m_photo = vcardIQ.photo();
    d->m_photoType = PhotoType::Png;//iq.photoType();
    d->m_description = vcardIQ.description();

    d->m_vcard->m_birthday = vcardIQ.birthday();
    d->m_vcard->m_email = vcardIQ.email();
    d->m_vcard->m_address = vcardIQ.addresses().isEmpty() ? "" : vcardIQ.addresses().first().street();
    d->m_vcard->m_url = vcardIQ.url();
}

VCard &ContactModel::vcard() const
{
    if(d->m_vcard == NULL){
        d->m_vcard = RosterModel::instance().loadVCard(bareJid());
    }

    return *d->m_vcard;
}

const char *ContactModel::qmlName()
{
    return QmlNames::SubModelType::ContactModel;
}

AvailableStatus ContactModel::availableStatus() const
{
    return d->m_availableStatus;
}

QString ContactModel::bareJid() const
{
    return d->m_bareJid;
}

QString ContactModel::group() const
{
    return d->m_group;
}

QDate ContactModel::birthday() const
{
    return vcard().m_birthday;
}

QString ContactModel::description() const
{
    return d->m_description;
}

QString ContactModel::email() const
{
    return vcard().m_email;
}

QString ContactModel::remarkName() const
{
    return d->m_remarkName;
}

QString ContactModel::nickName() const
{
    return d->m_nickName;
}

QString ContactModel::photo() const
{
    return d->m_photo;
}

PhotoType ContactModel::photoType() const
{
    return d->m_photoType;
}

QString ContactModel::url() const
{
    return vcard().m_url;
}

QString ContactModel::thumbnail() const
{
    return d->m_thumbnail;
}

QString ContactModel::telephone() const
{
    return vcard().m_telephone;
}

QString ContactModel::mobile() const
{
    return vcard().m_mobile;
}

QString ContactModel::address() const
{
    return vcard().m_address;
}

ContactModel &ContactModel::setAvailableStatus(X::AvailableStatus availableStatus)
{
    if (d->m_availableStatus == availableStatus)
        return *this;

    d->m_availableStatus = availableStatus;
    emit availableStatusChanged(availableStatus);

    return *this;
}

ContactModel &ContactModel::setGroup(QString group)
{
    if (d->m_group == group)
        return *this;

    d->m_group = group;
    emit groupChanged(group);

    return *this;
}

ContactModel &ContactModel::setBirthday(QDate birthday)
{
    if(vcard().m_birthday == birthday)
        return *this;

    vcard().m_birthday = birthday;
    emit birthdayChanged(birthday);

    return *this;
}

ContactModel &ContactModel::setDescription(QString description)
{
    if(d->m_description == description)
        return *this;

    d->m_description = description;
    emit descriptionChanged(description);

    return *this;
}

ContactModel &ContactModel::setEmail(QString email)
{
    if(vcard().m_email == email)
        return *this;

    vcard().m_email = email;
    emit emailChanged(email);

    return *this;
}

ContactModel &ContactModel::setAddress(QString address)
{
    if(vcard().m_address == address)
        return *this;

    vcard().m_address = address;
    emit addressChanged(address);

    return *this;
}

ContactModel &ContactModel::setRemarkName(QString remarkName)
{
    if(remarkName == d->m_remarkName)
        return *this;

    d->m_remarkName = remarkName;
    emit remarkNameChanged(remarkName);

    return *this;
}

ContactModel &ContactModel::setNickName(QString nickName)
{
    if(d->m_nickName == nickName)
        return *this;

    d->m_nickName = nickName;
    emit nickNameChanged(nickName);

    return *this;
}

/**
 * @brief 设置照片的时候，顺便设置缩略图。
 * @param photo
 * @return
 */
ContactModel &ContactModel::setPhoto(QString photo)
{
    QString temp = photo.startsWith("/") ? "file://" : "file:///";
    if(d->m_photo == temp)
        return *this;

    QString thumbnailPhoto = PhotoUtil::instance().createThumbnail(photo, 32, 32);

    d->m_photo = photo;
    d->m_thumbnail = thumbnailPhoto;

    emit photoChanged(d->m_photo);
    emit thumbnailChanged(d->m_thumbnail);

    return *this;
}

ContactModel &ContactModel::setPhotoType(PhotoType photoType)
{
    if(photoType == d->m_photoType)
        return *this;

    d->m_photoType = photoType;
    emit photoTypeChanged(photoType);

    return *this;
}

ContactModel &ContactModel::setUrl(QString url)
{
    if(vcard().m_url == url)
        return *this;

    vcard().m_url = url;
    emit urlChanged(url);

    return *this;
}

ContactModel &ContactModel::setTelephone(QString telephone)
{
    if (vcard().m_telephone == telephone)
        return *this;

    vcard().m_telephone = telephone;
    emit telephoneChanged(telephone);

    return *this;
}

ContactModel &ContactModel::setMobile(QString mobile)
{
    if (vcard().m_mobile == mobile)
        return *this;

    vcard().m_mobile = mobile;
    emit mobileChanged(mobile);

    return *this;
}

X_END_NAMESPACE
